/**
 * GWUser Controller层
 */
app.controller('gwUserCtrl', function ($scope, gwUserService) {
    var searchName = '';

    /**
     * 初始化发送队列下拉框
     */
    function initSendQueueList(id,name){
        if(id > 0 && name != null && name.length > 0){
            gwUserService.initSendQueueList({
                action:'initSendQueueList',
                id:id,
                name:name
            },function(data){
                if(data.state == 'SUCCESS'){
                    $scope.listQueueForSend = data.data.queueVO;
                }else{
                    message_alert('提示',data.reason,null);
                }
            });
        }else{
            gwUserService.initSendQueueList({
                action : 'initSendQueueList'
            },function(data){
                if(data.state == 'SUCCESS'){
                    $scope.listQueueForSend = data.data.queueVO;
                }else{
                    message_alert('提示',data.reason,null);
                }
            });
        }
    }

    /**
     * 初始化接收队列下拉框
     */
    function initRecvQueueList(id,name){
        if(id > 0 && name != null && name.length > 0){
            gwUserService.initRecvQueueList({
                action:'initRecvQueueList',
                id:id,
                name:name
            },function(data){
                if(data.state == 'SUCCESS'){
                    $scope.listQueueForRecv = data.data.queueVO;
                }else{
                    message_alert('提示',data.reason,null);
                }
            });
        }else{
            gwUserService.initRecvQueueList({
                action:'initRecvQueueList'
            },function(data){
                if(data.state == 'SUCCESS'){
                    $scope.listQueueForRecv = data.data.queueVO;
                }else{
                    message_alert('提示',data.reason,null);
                }
            });
        }
    }

    /**
     * 页面初始化查询网关用户
     */
    gwUserService.findGWUserByName({
        action: 'findGWUserByName',
        currentPage: 1,
        pageSize: 15
    }, function (data) {
        //展示数据
        if (data.state == 'SUCCESS') {
            $scope.gwUserList = data.data.gwUserVO;
            pageOperation($scope,data.pageInfo);
        } else {
            message_alert('提示',data.reason,null);
        }
    });

    /**
     * 根据网关用户名查询用户
     */
    $scope.findGWUserByName = function () {
        searchName = $scope.searchName;
        $scope.pageSize = $scope.selectPageSize;
        gwUserService.findGWUserByName({
            action: 'findGWUserByName',
            name: searchName,
            currentPage: 1,
            pageSize: $scope.pageSize
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.gwUserList = data.data.gwUserVO;
                pageOperation($scope,data.pageInfo);
            } else {
                message_alert('提示',"查询失败(" + data.reason + ")",null);
            }
        });
    }

    /**
     * 通过分页按钮查询
     * @param object
     */
    $scope.findByCondition = function (object) {
        $scope.pageSize = $scope.selectPageSize;
        gwUserService.findGWUserByName({
            action: 'findGWUserByName',
            name: searchName,
            currentPage: $scope.selPage,
            pageSize: $scope.pageSize
        }, function (data) {
            if (data.state == 'SUCCESS') {
                $scope.gwUserList = data.data.gwUserVO;
                pageOperation($scope,data.pageInfo);
            } else {
                message_alert('提示',"查询失败(" + data.reason + ")",null);
            }
        });
    };

    $scope.cancel = function () {
        $('#dialogGWUser').modal('hide');
    };

    /**
     * 校验网关用户名称
     */
    $scope.checkGWUserName = function (userName) {
        gwUserService.checkGWUserName({
            action: 'checkGWUserName',
            userName: userName
        }, function (data) {
            if (data.state != 'SUCCESS') {
                if (data.code == 0) {
                    $scope.userForm.userName.$setValidity("unique", false);
                }
            }else {
                $scope.userForm.userName.$setValidity("unique", true);
            }
        });
    };

    /**
     * 校验网关用户ID
     * @param userId
     */
    $scope.checkGWUserId = function(userId){
        gwUserService.checkGWUserId({
            action: 'checkGWUserId',
            userId: userId
        },function(data){
            if (data.state != 'SUCCESS') {
                if (data.code == 0) {
                    $scope.userForm.userId.$setValidity("unique", false);
                }
            }else {
                $scope.userForm.userId.$setValidity("unique", true);
            }
        });
    };

    /**
     * 校验网关用户ID
     */
    $scope.checkName = function (userName) {
        gwUserService.checkGWUserName({
                action: 'checkGWUserName',
                userName: userName
            }, function (data) {
                if (data.state != 'SUCCESS') {
                    if (data.code == 0) {
                        $scope.userForm.userName.$setValidity("unique", false);
                        message_alert('提示',data.reason,null);
                    }
                }else {
                    $scope.userForm.userName.$setValidity("unique", true);
                }
            }
        )
    };


    /**
     * 新增用户
     */
    $scope.createGWUser = function () {

        // 初始化发送队列下拉框
        initSendQueueList(0,null);

        // 初始化接收队列下拉框
        initRecvQueueList(0,null);
        $('#glyphicon').removeClass("glyphicon glyphicon-pencil");
        $('#glyphicon').removeClass("glyphicon glyphicon-plus");
        $('#glyphicon').addClass("glyphicon glyphicon-plus");
        $('#dialogGWUser').on('show.bs.modal',function(){

            $('#userName').removeAttr('disabled','disabled');
            $('#userId').removeAttr('disabled','disabled');
            $scope.userForm.userName.$setValidity("enable",true);

            // 初始化用户新增界面
            $scope.title = '新增用户';

            // 初始化网关用户实体
            $scope.gwUser.id = 0;
            $scope.gwUser.userId = '';
            $scope.gwUser.userName = '';
            $scope.gwUser.userIP = '';
            $scope.gwUser.sendQueueId = '0';
            $scope.gwUser.recvQueueId = '0';
            $scope.gwUser.downlinkMsg = '0';
            $scope.gwUser.downLinkForward = '0';
        });
        $('#dialogGWUser').modal('show');
    };

    /**
     * 修改用户
     * @param queue
     */
    $scope.updateGWUser = function (gwUser) {

        // 初始化发送队列下拉框
        initSendQueueList(gwUser.sendQueueId,gwUser.sendQueueName);

        // 初始化接收队列下拉框
        initRecvQueueList(gwUser.recvQueueId,gwUser.recvQueueName);
        $('#glyphicon').removeClass("glyphicon glyphicon-pencil");
        $('#glyphicon').removeClass("glyphicon glyphicon-plus");
        $('#glyphicon').addClass("glyphicon glyphicon-pencil");
        $('#dialogGWUser').on('show.bs.modal',function(){

            // 初始化用户修改界面
            $scope.title = '修改用户';
            $('#userName').attr('disabled','disabled');
            $('#userId').attr('disabled','disabled');

            if(gwUser.status){
                $scope.userForm.userName.$setValidity("enable",false);
            }else{
                $scope.userForm.userName.$setValidity("enable",true);
            }

            // 初始化网关用户实体
            $scope.gwUser.id = gwUser.id;
            $scope.gwUser.userId = gwUser.userId;
            $scope.gwUser.userName = gwUser.userName;
            $scope.gwUser.userIP = gwUser.userIP;
            $scope.gwUser.sendQueueId = gwUser.sendQueueId.toString();
            $scope.gwUser.recvQueueId = gwUser.recvQueueId.toString();
            $scope.gwUser.downlinkMsg = gwUser.downlinkMsg.toString();
            $scope.gwUser.downLinkForward = gwUser.downLinkForward ? '1':'0';
        });
        $('#dialogGWUser').modal('show');
    };

    /**
     * 保存网关用户
     * @param gwUser
     */
    $scope.save = function (gwUser){

        // 校验是否选择发送队列
        if (gwUser.sendQueueId == 0){
            message_alert('提示','请选择发送队列',null);
            return;
        }

        // 校验是否选择接收队列
        if (gwUser.recvQueueId == 0){
            message_alert('提示','请选择接收队列',null);
            return;
        }
        if (gwUser.id == 0) {
            gwUserService.save({
                userId:gwUser.userId,
                userName: gwUser.userName,
                userIP: gwUser.userIP,
                sendQueueId: gwUser.sendQueueId,
                recvQueueId: gwUser.recvQueueId,
                downlinkMsg: gwUser.downlinkMsg,
                downLinkForward: gwUser.downLinkForward
            },{},function (data) {
                if (data.state == 'SUCCESS') {
                    message_alert('提示','保存成功',function () {
                        $scope.cancel();
                        $scope.findByCondition();
                    });
                } else {
                    message_alert('提示',data.reason,null);
                }
            }, $scope.requestError);
        }else{
            var downLinkForward;
            if(gwUser.downLinkForward=="0"){
                downLinkForward = false;
            }else{
                downLinkForward = true;
            }
            gwUserService.updateUser({
                id: gwUser.id,
                userId:gwUser.userId,
                userName: gwUser.userName,
                userIP: gwUser.userIP,
                sendQueueId: gwUser.sendQueueId,
                recvQueueId: gwUser.recvQueueId,
                downlinkMsg: gwUser.downlinkMsg,
                downLinkForward:downLinkForward
            },function (data) {
                if (data.state == 'SUCCESS') {
                    message_alert('提示','保存成功',function () {
                        $scope.cancel();
                        $scope.findByCondition();
                    });
                } else {
                    message_alert('提示',data.reason,null);
                }
            }, $scope.requestError);
        }
    };

    /**
     * 删除网关用户
     * @param gwUser
     */
    $scope.deleteGWUser = function (gwUser) {
        if (gwUser.status == 0) {
            message_confirm('提示','确认是否删除该用户配置？',
                function (result) {
                if (result) {
                    message_confirm('提示','确认是否删除该用户队列信息？',
                        function (result) {
                            gwUserService.delete({
                                id: gwUser.id,
                                flag:result
                            }, function (data) {
                                if (data.state == 'SUCCESS') {
                                    message_alert('提示','删除网关用户成功', function () {
                                        $scope.cancel();
                                        $scope.findByCondition();
                                    });
                                    if(data.code != 0){
                                        message_alert('提示',data.reason,null);
                                    }
                                } else {
                                    message_alert('提示',"删除失败(" + data.reason + ")",null);
                                }
                            }, $scope.requestError);
                        });
                }
            });
        } else {
            message_alert('提示','已上线的用户禁止删除，请先下线再行删除！',null);
        }

    };

    /**
     * 网关用户上线
     * @param gwUser
     */
    $scope.online = function (gwUser) {
        gwUserService.online({
            action: 'gwUserOnline',
            id: gwUser.id
        }, {}, function (data) {
            if (data.state == 'SUCCESS') {
                message_alert('提示','操作成功',function () {
                    $scope.cancel();
                    $scope.findByCondition();
                });
            } else {
                message_alert('提示',"操作失败(" + data.reason + ")",null);
                $scope.findByCondition();
            }
        }, $scope.requestError);
    };

    /**
     * 网关用户下线
     * @param gwUser
     */
    $scope.offline = function (gwUser) {
        gwUserService.offline({
            action: 'gwUserOffline',
            id: gwUser.id
        },{},function (data) {
            if (data.state == 'SUCCESS') {
                message_alert('提示','操作成功', function () {
                    $scope.cancel();
                    $scope.findByCondition();
                });
            } else {
                message_alert('提示',"操作失败(" + data.reason + ")",null);
                $scope.findByCondition();
            }
        }, $scope.requestError);
    };

    /**
     * 更新网关用户
     */
    $scope.updateUserList = function(){
        gwUserService.updateUserList({
            action: 'updateUserList'
        }, {}, function (data) {
            if (data.state == 'SUCCESS') {
                message_alert('提示','网关用户更新成功', function () {
                    $scope.findByCondition();
                    $scope.cancel();
                });
            } else {
                message_alert('提示',data.reason,null);
            }
        }, $scope.requestError)
    };

    /**
     * 下载用户列表
     */
    $scope.downloadUserList = function(){
        location.href = "GWUser/downloadUserList";
    };
});